package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.MaterialInventory;
import com.ruoyi.factorydata.service.IMaterialInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料库存信息Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/warehouse/materialInventory")
public class MaterialInventoryController extends BaseController {
    @Autowired
    private IMaterialInventoryService materialInventoryService;

    /**
     * 查询物料库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialInventory materialInventory) {
        startPage();
        List<MaterialInventory> list = materialInventoryService.selectMaterialInventoryList(materialInventory);
        return getDataTable(list);
    }

    /**
     * 导出物料库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:export')")
    @Log(title = "物料库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialInventory materialInventory) {
        List<MaterialInventory> list = materialInventoryService.selectMaterialInventoryList(materialInventory);
        ExcelUtil<MaterialInventory> util = new ExcelUtil<MaterialInventory>(MaterialInventory.class);
        util.exportExcel(response, list, "物料库存信息数据");
    }

    /**
     * 获取物料库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialInventoryService.selectMaterialInventoryById(id));
    }

    /**
     * 获取产成品库存详细信息
     */
    @GetMapping(value = "/scanner/{inventoryReceiptId}")
    public AjaxResult getScannerInfo(@PathVariable("inventoryReceiptId") Long inventoryReceiptId) {
        return success(materialInventoryService.getScannerInfo(inventoryReceiptId));
    }
    /**
     * 新增物料库存信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:add')")
    @Log(title = "物料库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialInventory materialInventory) {
        return toAjax(materialInventoryService.insertMaterialInventory(materialInventory));
    }

    /**
     * 修改物料库存信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:edit')")
    @Log(title = "物料库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialInventory materialInventory) {
        return toAjax(materialInventoryService.updateMaterialInventory(materialInventory));
    }

    /**
     * 删除物料库存信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:remove')")
    @Log(title = "物料库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialInventoryService.deleteMaterialInventoryByIds(ids));
    }


    /**
     * 查询物料库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:materialInventory:list')")
    @GetMapping("/list/count")
    public AjaxResult listInventoryCount(MaterialInventory materialInventory) {
        Long listInventoryCount = materialInventoryService.countMaterialInventoryList(materialInventory);
        return AjaxResult.success(listInventoryCount);
    }


}
